package com.game.modular.game.battle.info.base;

import com.game.modular.game.playerMatch.domain.PvpCity;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * pvp玩家信息基础服务
 */
public class PvpPlayerInfoBaseServiceImpl {
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    private HashOperations<Object, String, PvpPlayerInfo> hashOperations;
    private final static String redisKey = "pvpMatch:%d:playerInfo";
    public void init(
            @NotNull  PvpCity city
    ) {
        Long uid = city.getOwnerUid();
        Long matchId = city.getMatchId();

        Set<Long> currentCity = new HashSet<>();
        currentCity.add(city.getCoreCityId());

        Set<Long> alliesUidList = new HashSet<>();
        alliesUidList.add(uid);


        PvpPlayerInfo pvpPlayerInfo = PvpPlayerInfo.builder()
                .playerId(uid)
                .matchId(matchId)
                .joinTime(new Date().getTime())
                //////////////////////////////////////////////////////////////////////////////////////////////// 生产力
                .grainRateCombined(city.getGrainRate())
                .copperRateCombined(city.getCopperRate())
                //////////////////////////////////////////////////////////////////////////////////////////////// 联盟信息
                .alliesUidList(alliesUidList)
                //////////////////////////////////////////////////////////////////////////////////////////////// 存活信息
                .maxCity(1)
                .currentCity(currentCity)
                //////////////////////////////////////////////////////////////////////////////////////////////// 结算信息
                .totalExp(0L)
                .totalIntegral(0L)
                .killedBy(null)
                .survivalTime(0L)
                .killedPlayers(new HashSet<Long>())
                .isExit(0)
                .isDead(0)


                .build();
        savaCache(pvpPlayerInfo);
    }

    public void savaCache(PvpPlayerInfo pvpPlayerInfo) {
        String key = String.format(redisKey, pvpPlayerInfo.getMatchId());
        hashOperations.put(key, pvpPlayerInfo.getPlayerId().toString(), pvpPlayerInfo);
        redisTemplate.expire(key, 1, TimeUnit.DAYS);
    }

    public List<PvpPlayerInfo> listCache(Long matchId) {
        String key = String.format(redisKey, matchId);
        return new ArrayList<>(hashOperations.entries(key).values());
    }

    public Map<String, PvpPlayerInfo> mapCache(Long matchId) {
        String key = String.format(redisKey, matchId);
        return hashOperations.entries(key);
    }

    public PvpPlayerInfo get(Long matchId, Long ownerUid) {
        String key1 = String.format(redisKey, matchId);
        return hashOperations.get(key1, ownerUid.toString());
    }

    public void update(PvpPlayerInfo pvpPlayerInfo) {
        String key1 = String.format(redisKey, pvpPlayerInfo.getMatchId());
        String key2 = pvpPlayerInfo.getPlayerId().toString();
        hashOperations.put(key1, key2, pvpPlayerInfo);
        redisTemplate.expire(key1, 1, TimeUnit.DAYS);
    }

    public void removeCache(Long matchId, Long ownerUid) {
        String key1 = String.format(redisKey, matchId);
        String key2 = ownerUid.toString();
        hashOperations.delete(key1, key2);
    }
}
